package com.qnbc.mystery.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qnbc.mystery.pojo.domain.ProArea;
import com.qnbc.mystery.pojo.response.ProAreaResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qnbc.mystery.mapper.ProAreaMapper;
import com.qnbc.mystery.service.IProAreaService;

/**
 * 地区码Service业务层处理
 *
 * @author guzj
 * @date 2024-01-24
 */
@Service
public class ProAreaServiceImpl implements IProAreaService {
    @Autowired
    private ProAreaMapper proAreaMapper;

    /**
     * 查询地区码
     *
     * @param areaid 地区码主键
     * @return 地区码
     */
    @Override
    public ProArea selectProAreaByAreaid(Long areaid) {
        return proAreaMapper.selectProAreaByAreaid(areaid);
    }

    /**
     * 查询地区码列表
     *
     * @param proArea 地区码
     * @return 地区码
     */
    @Override
    public List<ProArea> selectProAreaList(ProArea proArea) {
        return proAreaMapper.selectProAreaList(proArea);
    }

    /**
     * 新增地区码
     *
     * @param proArea 地区码
     * @return 结果
     */
    @Override
    public int insertProArea(ProArea proArea) {
        return proAreaMapper.insertProArea(proArea);
    }

    /**
     * 修改地区码
     *
     * @param proArea 地区码
     * @return 结果
     */
    @Override
    public int updateProArea(ProArea proArea) {
        return proAreaMapper.updateProArea(proArea);
    }

    /**
     * 批量删除地区码
     *
     * @param areaids 需要删除的地区码主键
     * @return 结果
     */
    @Override
    public int deleteProAreaByAreaids(Long[] areaids) {
        return proAreaMapper.deleteProAreaByAreaids(areaids);
    }

    /**
     * 删除地区码信息
     *
     * @param areaid 地区码主键
     * @return 结果
     */
    @Override
    public int deleteProAreaByAreaid(Long areaid) {
        return proAreaMapper.deleteProAreaByAreaid(areaid);
    }

    @Override
    public List<ProAreaResponse> erjiList(ProArea proArea) {
        List<ProAreaResponse> proAreaResponseList = new ArrayList<>();
        proArea.setLevel(1L);
        List<ProArea> proAreas = proAreaMapper.selectProAreaList(proArea);
        for (ProArea area : proAreas) {
            ProAreaResponse proAreaResponse = new ProAreaResponse();
            proAreaResponse.setValue(String.valueOf(area.getAreaid()));
            proAreaResponse.setLabel(area.getAreaname());

            ProArea proAreaerji = new ProArea();
            proAreaerji.setLevel(2L);
            proAreaerji.setParentid(area.getAreaid());
            List<ProArea> proAreasErji = proAreaMapper.selectProAreaList(proAreaerji);
            List<ProAreaResponse> proAreaErjiList = new ArrayList<>();
            for (ProArea proArea1 : proAreasErji) {
                ProAreaResponse proAreaResponseErji = new ProAreaResponse();
                proAreaResponseErji.setValue(String.valueOf(proArea1.getAreaid()));
                proAreaResponseErji.setLabel(proArea1.getAreaname());
                proAreaErjiList.add(proAreaResponseErji);
            }

            proAreaResponse.setChildren(proAreaErjiList);
            proAreaResponseList.add(proAreaResponse);
        }

        return proAreaResponseList;
    }
}
